package com.ww.word.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ww.core.activity.MyActivity;
import com.ww.word.R;

public class PractiseActivity extends MyActivity {
	private String common;
	private Button onclick1, onclick2, onclick3, onclick4, onclick5, onclick6, onclick7, onclick8;
	private TextView txt1, txt2, txt3, txt4;
	private String other, other2;
	private int type2;
	private LinearLayout linearLayout1, linearLayout2, linearLayout3, linearLayout4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_practise);
		setTitle("练习");
		common = getString(R.string.txt).toString();
		findView();
		String name = getIntent().getStringExtra("name");
		String[] group = getIntent().getStringExtra("group").split(" ");
		if (group.length > 0) {
			if (group[0].startsWith(name)) {
				type = 1;
			} else {
				type = 2;
			}
			other = group[0].replace(name, "");
			ffff(name, getStrings(other));

			if (group.length > 1) {
				if (group[1].startsWith(name)) {
					type2 = 1;
				} else {
					type2 = 2;
				}
				other2 = group[1].replace(name, "");
				ffff2(name, getStrings(other2));
			} else {
				linearLayout3.setVisibility(View.GONE);
				linearLayout4.setVisibility(View.GONE);
			}
		} else {
			linearLayout1.setVisibility(View.GONE);
			linearLayout2.setVisibility(View.GONE);
			linearLayout3.setVisibility(View.GONE);
			linearLayout4.setVisibility(View.GONE);
		}
	}

	private void findView() {
		txt1 = (TextView) findViewById(R.id.txt1);
		txt2 = (TextView) findViewById(R.id.txt2);
		txt3 = (TextView) findViewById(R.id.txt3);
		txt4 = (TextView) findViewById(R.id.txt4);

		linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
		linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
		linearLayout3 = (LinearLayout) findViewById(R.id.linearLayout3);
		linearLayout4 = (LinearLayout) findViewById(R.id.linearLayout4);

		onclick1 = (Button) findViewById(R.id.onclick1);
		onclick2 = (Button) findViewById(R.id.onclick2);
		onclick3 = (Button) findViewById(R.id.onclick3);
		onclick4 = (Button) findViewById(R.id.onclick4);
		onclick5 = (Button) findViewById(R.id.onclick5);
		onclick6 = (Button) findViewById(R.id.onclick6);
		onclick7 = (Button) findViewById(R.id.onclick7);
		onclick8 = (Button) findViewById(R.id.onclick8);
	}

	private String[] getStrings(String other) {
		String[] strs = new String[4];
		int size = common.length() - 2;
		for (int i = 0; i < 4; i++) {
			int index = (int) (Math.random() * size);
			strs[i] = common.substring(index, index + 1);
		}

		int xxx = (int) (Math.random() * 4);
		strs[xxx] = other;
		return strs;
	}

	private void ffff(String str, String[] strs) {
		if (type == 2) {
			txt1.setText("");
			txt1.setBackgroundResource(R.drawable.crossframe_line);
			txt1.setTextColor(Color.BLUE);
			txt2.setText(str);
			txt2.setBackgroundResource(R.drawable.pw_bg);
			txt2.setTextColor(Color.BLACK);
		} else {
			txt2.setText("");
			txt2.setBackgroundResource(R.drawable.crossframe_line);
			txt2.setTextColor(Color.BLUE);
			txt1.setText(str);
			txt1.setBackgroundResource(R.drawable.pw_bg);
			txt1.setTextColor(Color.BLACK);
		}

		onclick1.setText(strs[0]);
		onclick2.setText(strs[1]);
		onclick3.setText(strs[2]);
		onclick4.setText(strs[3]);

	}

	private void ffff2(String str, String[] strs) {
		if (type2 == 2) {
			txt3.setText("");
			txt3.setBackgroundResource(R.drawable.crossframe_line);
			txt3.setTextColor(Color.BLUE);
			txt4.setText(str);
			txt4.setBackgroundResource(R.drawable.pw_bg);
			txt3.setTextColor(Color.BLACK);
		} else {
			txt4.setText("");
			txt4.setBackgroundResource(R.drawable.crossframe_line);
			txt4.setTextColor(Color.BLUE);
			txt3.setText(str);
			txt3.setBackgroundResource(R.drawable.pw_bg);
			txt3.setTextColor(Color.BLACK);
		}

		onclick5.setText(strs[0]);
		onclick6.setText(strs[1]);
		onclick7.setText(strs[2]);
		onclick8.setText(strs[3]);

	}

	/**
	 * 杀死所有选择的进程
	 * 
	 * @param view
	 */
	public void onclick(View view) {
		String str = ((Button) view).getText().toString();
		if (type == 2) {
			if (other.equals(str)) {
				txt1.setText(str);
				showMessage("恭喜");
			} else {
				showMessage("错了");
			}
		} else {
			if (other.equals(str)) {
				txt2.setText(str);
				showMessage("恭喜");
			} else {
				showMessage("错了");
			}
		}
	}

	public void onclick2(View view) {
		String str = ((Button) view).getText().toString();
		if (type2 == 2) {
			if (other2.equals(str)) {
				txt3.setText(str);
				showMessage("恭喜");
			} else {
				showMessage("错了");
			}
		} else {
			if (other2.equals(str)) {
				txt4.setText(str);
				showMessage("恭喜");
			} else {
				showMessage("错了");
			}
		}
	}

}
